/**
 *
 */

layui.use('table', function () {
    var table = layui.table;

    //第一个实例
    table.render({
        elem: '#UserMessage',
        id:'managerMessage',
        height: 500,
        cellMinWidth: 100,
        url: '/manager/managerIndex.do',//数据接口
        page: true,//开启分页
        cols: [[ //表头
            {
                field: 'managerMessageId',
                title: '消息编号',
                sort: true,
                align: 'center'
            }, {
                field: 'userName',
                title: '发布者名称',
                align: 'center',
                templet: function (value) {
                    return '<span>' + value.user.userName + '</span>'
                }
            }, {
                field: 'EssayTitle',
                title: '文章标题',
                align: 'center',
                templet: function (value) {
                    return '<span>' + value.essay.title + '</span>'
                }
            }, {
                field: 'EssayPublishDate',
                title: '文章发布日期',
                align: 'center',
                sort: true,
                templet: function (value) {
                    return '<span>' + value.essay.publishDate + '</span>'
                }
            }, {
                field: 'sensitiveWords',
                title: '敏感词汇',
                align: 'center'
            }, {
                field: 'EssayCategory',
                title: '文章类别',
                align: 'center',
                templet: function (value) {
                    if ('downLoadEssay' == value.essay.category) {
                        return '下载帖'
                    } else {
                        return '普通帖'
                    }
                }

            }, {
                field: 'auditing',
                title: '审核状态',
                align: 'center',
                templet: function (value) {
                    if (1 == value.essay.auditing) {
                        return '已审核'
                    } else {
                        return '待审核'
                    }
                }

            }, {
                fixed: 'right',
                title: '操作',
                align: 'center',
                toolbar: '#barDemo',
                width: 150
            }]]
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        console.log(data);
        if (obj.event === 'delete') {
            layer.confirm('文章未审核，确定删除吗', function (index) {
                var managerMessageId = data.managerMessageId;
                $.ajax({
                    type: 'POST',
                    url: '/manager/deleteMessage.do',
                    data: {
                        managerMessageId: managerMessageId
                    },
                    success: function (value) {
                        if (value == "success") {
                            layer.msg("删除成功", {icon: 1, time: 500}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg("服务器好像出错了，请稍后重试", {icon: 2, time: 500});
                        }
                    }
                })
                layer.close(index);
            });
        } else if (obj.event === 'auditing') {
            console.log(data)
            var essayId = data.essayId;
            var messageId = data.managerMessageId;
            parent.frames["main"].window.location.href = "/essay/auditingEssay.do?essayId=" + essayId+"&messageId="+ messageId;
        }
    });

});